using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using XNgine;
using XNgine.ObjectManagement;
using XNgine.ObjectManagement.Infos;
using XNgine.RenderManagement;
using XNgine_Test_Game.Objects;

namespace XNgine_Test_Game
{
    public class RenderProvider : IRenderableObjectsProvider
    {
        readonly List<PositionedRenderable> renderables = new List<PositionedRenderable>();

        public RenderProvider()
        {
            var plane = new XPlane("plane1");
            var box = new Box("box1");

            InsertObject(plane, new Vector3(0));

            InsertAtAxes(box, 10);

            //Without optimizations 2500 max objects
            InsertAtRandom(box, 50, 50);

        }

        private void InsertAtRandom(IRenderable obj, int number, int radius)
        {
            Random r = new Random();

            for (int i = 0; i < number; i++)
            {

                int x = r.Next(-radius, radius);
                int y = r.Next(-radius, radius);
                int z = r.Next(-radius, radius);

                InsertObject(obj, new Vector3(x, y, z));
            }
        }

        private void InsertAtAxes(IRenderable obj, int distance)
        {
            InsertObject(obj, Vector3.Zero);
            InsertObject(obj, new Vector3(distance, 0, 0));
            InsertObject(obj, new Vector3(0, distance, 0));
            InsertObject(obj, new Vector3(0, 0, distance));

            InsertObject(obj, new Vector3(-distance, 0, 0));
            InsertObject(obj, new Vector3(0, -distance, 0));
            InsertObject(obj, new Vector3(0, 0, -distance));
        }

        private void InsertObject(IRenderable obj, Vector3 position)
        {
            var dimensionInfo = new DimensionInfo
            {
                Position = position,
                Orientation = Quaternion .Identity,
                Scaling = new Vector3(1)
            };
            renderables.Add(new PositionedRenderable { Object = obj, DimensionInfo = dimensionInfo });
        }

        public IEnumerable<PositionedRenderable> GetObjectsForRendering(BoundingFrustum frustum)
        {
            return renderables;
        }
    }
}